package com.yeyaomai.dksns.flowis.servlet;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.yeyaomai.dksns.flowis.domain.FlowAuth;
import com.yeyaomai.dksns.flowis.service.FlowAuthService;

@Controller
@RequestMapping("/authmanage/*")
public class FlowAuthController {
	
	@Autowired
	private FlowAuthService flowAuthService;
	
	@RequestMapping(value = "/toaddauth")
	public String toAddAuth() {
		return "add-auth";
	}

	@RequestMapping(value = "/toupdateflowauth")
	public ModelAndView toUpdateFlowAuth(HttpServletRequest request) {
		String authId = request.getParameter("authId");
		 FlowAuth flowAuth = flowAuthService.getFlowAuthByAuthId(Long.valueOf(authId));
		ModelAndView view = new ModelAndView("auth_edit");
		view.addObject("flowAuth", flowAuth);
		return view;
	}
	
	@RequestMapping(value = "/addauth")
	public String addFlowUser(FlowAuth flowAuth, HttpServletRequest request) {
		flowAuth.setStatus(0);
		flowAuthService.addFlowAuth(flowAuth);
		return "redirect:/authmanage/listauth?number=1";
	}

	@RequestMapping(value = "/updateauth")
	public String updateFlowUser(FlowAuth flowAuth, HttpServletRequest request) {
		flowAuthService.updateFlowAuth(flowAuth);
		return "redirect:/authmanage/listauth?number=1";
	}
	

	@RequestMapping(value = "/listauth")
	public ModelAndView listFlowAuth(HttpServletRequest request) {

		ModelAndView view = new ModelAndView("authlist");
		String number = request.getParameter("number");
		Page<FlowAuth> result = flowAuthService.listFlowAuth(Integer.valueOf(number));
		view.addObject("result", result);
		view.addObject("number", number);
		return view;
	}

	@RequestMapping(value = "/isexistflowauth")
	public void isExistFlowAuth(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String authName = request.getParameter("authName");
		boolean isExistFlowAuth = flowAuthService.isExistFlowAuth(authName
				.trim());
 		String result = "";
		if (isExistFlowAuth) {
			result = "0";
		} else {
			result = "1";
		}
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().print(result);
	}
	
	@RequestMapping(value = "/delflowauth")
	public String delFlowAuth(HttpServletRequest request) {
		String authId = request.getParameter("authId");
		flowAuthService.deleteFlowAuth(Long.valueOf(authId));
		return "redirect:/authmanage/listauth?number=1";
	}
	

	@RequestMapping(value = "/updatestatus")
	public String updateStatus(HttpServletRequest request) {
		String authId = request.getParameter("authId");
		String status = request.getParameter("status");
		FlowAuth flowAuth = new FlowAuth();
		flowAuth.setAuthId(Integer.valueOf(authId));
		flowAuth.setStatus(Integer.valueOf(status));
		flowAuthService.updateFlowAuth(flowAuth);
		return "redirect:/authmanage/listauth?number=1";
	}
}
